part of 't3_map_controller.dart';

extension Polyline on T3MapController {
  /// 向地图添加线段
  T3Polyline addPolyline({required T3PolylineOptions options}) {
    print('lzf addPolyline dart ');
    final polyline = T3Polyline(options);
    _methodChannel.invokeMethod('polylines#add', jsonEncode(polyline.toJson()));
    _polylines.add(polyline);
    return polyline;
  }

  /// 更新指定线段，当前只支持更新坐标，样式暂不支持
  void updatePolyline({required T3Polyline polyline, required T3PolylineOptions newPolylineOptions}) {
    print('lzf updatePolyline ');
    polyline.update(points: newPolylineOptions.points);
    polyline.options = newPolylineOptions;
    _methodChannel.invokeMethod('polylines#update', jsonEncode(polyline.toJson()));
  }

  /// 移除对应的线段
  void removePolyline({required T3Polyline polyline}) {
    _methodChannel.invokeMethod('polylines#remove', jsonEncode(polyline.toJson()));
    _polylines.remove(polyline);
  }


  /// 添加面
  void addPolygon({required T3PolylineOptions options}) {
    print('t3map addPolygon ');
    final polyline = T3Polyline(options);
    var polygon_json = {
      "areaId": "EF320100000720220930000",
      "areaName": "南京南站",
      "scopeInfo": {
        "circleList": [{
          "circle" : 1,
          "center" : {
            "lng": "118.796655",
            "lat": "31.970431",
          },
          "radius" : 20,
        }],

        "polygonList": [{
          "outPolygon": [{
            "lng": "116.293413",
            "lat": "39.781892"
          }, {
            "lng": "116.391842",
            "lat": "39.787600"
          }, {
            "lng": "116.417932",
            "lat": "39.733187"
          }, {
            "lng": "116.338255",
            "lat": "39.704653"
          }],
          "inPolygonList": [
              {
                "lng": "116.313413",
                "lat": "39.761892"
              }, {
              "lng": "116.371842",
              "lat": "39.767600"
            }, {
              "lng": "116.397932",
              "lat": "39.753187"
            }, {
              "lng": "116.358255",
              "lat": "39.724653"
            }
          ]
        }]
      },
      "blockList": [{
        "blockId": 30000184,
        "blockName": "南京南站北广场",
        "pointIdList": [30000667]
      }, {
        "blockId": 30000185,
        "blockName": "南京南站南广场",
        "pointIdList": [30000668]
      }],
      "pointList": [{
        "pointId": 30000667,
        "pointName": "南京南站北进站口",
        "lng": "118.796655",
        "lat": "31.970431",
        "supplierPoiId": "B0FFG56XEN",
        "limitStopFlag": false,
        "limitStopTips": null,
        "guideFlag": false,
        "inFence": true,
        "streetScapeImage": null,
        "guideInfo": null
      }, {
        "pointId": 30000668,
        "pointName": "南京南站南进站口",
        "lng": "118.799391",
        "lat": "31.967172",
        "supplierPoiId": "B0FFF381MW",
        "limitStopFlag": false,
        "limitStopTips": null,
        "guideFlag": false,
        "inFence": true,
        "streetScapeImage": null,
        "guideInfo": null
      }]
    };

    _methodChannel.invokeMethod('Polygon#add', jsonEncode(polygon_json));
    // _polygon.add(polyline);
  }
  /// 移除面
  void removePolygon() {
    print('t3map removePolygon ');
    var polygon_json = {
      "areaId": "EF320100000720220930000",
      "areaName": "南京南站",
      "scopeInfo": {
        "circleList": [{
          "circle" : 1,
          "center" : {
            "lng": "118.796655",
            "lat": "31.970431",
          },
          "radius" : 20,
        }],

        "polygonList": [{
          "outPolygon": [{
            "lng": "116.293413",
            "lat": "39.781892"
          }, {
            "lng": "116.391842",
            "lat": "39.787600"
          }, {
            "lng": "116.417932",
            "lat": "39.733187"
          }, {
            "lng": "116.338255",
            "lat": "39.704653"
          }],
          "inPolygonList": [
            {
              "lng": "116.313413",
              "lat": "39.761892"
            }, {
              "lng": "116.371842",
              "lat": "39.767600"
            }, {
              "lng": "116.397932",
              "lat": "39.753187"
            }, {
              "lng": "116.358255",
              "lat": "39.724653"
            }
          ]
        }]
      },
      "blockList": [{
        "blockId": 30000184,
        "blockName": "南京南站北广场",
        "pointIdList": [30000667]
      }, {
        "blockId": 30000185,
        "blockName": "南京南站南广场",
        "pointIdList": [30000668]
      }],
      "pointList": [{
        "pointId": 30000667,
        "pointName": "南京南站北进站口",
        "lng": "118.796655",
        "lat": "31.970431",
        "supplierPoiId": "B0FFG56XEN",
        "limitStopFlag": false,
        "limitStopTips": null,
        "guideFlag": false,
        "inFence": true,
        "streetScapeImage": null,
        "guideInfo": null
      }, {
        "pointId": 30000668,
        "pointName": "南京南站南进站口",
        "lng": "118.799391",
        "lat": "31.967172",
        "supplierPoiId": "B0FFF381MW",
        "limitStopFlag": false,
        "limitStopTips": null,
        "guideFlag": false,
        "inFence": true,
        "streetScapeImage": null,
        "guideInfo": null
      }]
    };

    _methodChannel.invokeMethod('Polygon#remove', jsonEncode(polygon_json));
  }

  /// 交换路线层级
  void exchangePolyline(List<String> polylines) {
    _methodChannel.invokeMethod('polyline#exchange', jsonEncode(polylines));
  }

  /// 在可视区域内显示Polyline
  void showPolylines({required List<T3Polyline> polylines, EdgeInsets edgeInsets = EdgeInsets.zero, required bool animated}) {
    final json = {"polylines": polylines.map((e) => e.toJson()).toList() ,"animated": animated, "edgeInsets": _edgeInsetsToJson(edgeInsets)};
    _methodChannel.invokeMethod('polylines#show', jsonEncode(json));
  }
}
